﻿using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Reflection;

namespace VirtualTournaments.DataAccess.SqlServer.Properties
{
	internal static class LocalConfigurationManager
	{
		private static NameValueCollection appSettings = null;
		private static ConnectionStringSettingsCollection connectionStrings = null;

		internal static ConnectionStringSettingsCollection ConnectionStrings
		{
			get
			{
				if (connectionStrings == null)
				{
					connectionStrings = new ConnectionStringSettingsCollection();
					System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().GetBasePath());
					if (configuration.HasFile)
					{
						IEnumerator it = configuration.ConnectionStrings.ConnectionStrings.GetEnumerator();
						while (it.MoveNext())
						{
							connectionStrings.Add((ConnectionStringSettings)it.Current);
						}
					}
					else
					{
						connectionStrings = ConfigurationManager.ConnectionStrings;
					}
				}
				return connectionStrings;
			}
		}

		internal static NameValueCollection AppSettings
		{
			get
			{
				if (appSettings == null)
				{
					appSettings = new NameValueCollection();
					System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().GetBasePath());
					if (configuration.HasFile)
					{
						foreach (string appSettingKey in configuration.AppSettings.Settings.AllKeys)
						{
							appSettings.Add(appSettingKey, configuration.AppSettings.Settings[appSettingKey].Value);
						}
					}
					else
					{
						appSettings = ConfigurationManager.AppSettings;
					}
				}
				return appSettings;
			}
		}
	}
}
